<template>
  <BasicDrawer v-bind="$attrs" @register="registerDrawer" destroyOnClose showFooter title="现办现退" :width="drawerWidth" @ok="handleSubmit">
    <Description size="middle" class="mt-4" title="基础信息" :column="{ xxl: 4, xl: 3, lg: 3, md: 3, sm: 2, xs: 1 }" :data="userGoodsInfo.data" :schema="userGoodsInfoSchema" />
    <BasicForm @register="registerForm"></BasicForm>
  </BasicDrawer>
</template>
<script lang="ts" setup>
import { reactive, ref, unref } from 'vue';
import { BasicForm, useForm } from '@apps/antd-admin/src/components/Form/index';
import { BasicDrawer, useDrawerInner } from '@apps/antd-admin/src/components/drawer';
import { nowBackRecordFormSchema } from '../data/data';
import { getUserGoodsConsumptionInfo, nowRefundBackcardSub } from '../data/api';
import { useAppInject } from '@apps/antd-admin/src/hooks/web/use-app-inject';
import { DescItem, Description } from '@apps/antd-admin/src/components/description';

const { getIsMobile } = useAppInject();
const drawerWidth = ref('50%');
if (unref(getIsMobile)) {
  drawerWidth.value = '80%';
}
// 声明Emits
const emit = defineEmits(['success', 'register']);
let userGoodsInfo: Recordable = reactive({ data: {} });
const [registerForm, { resetFields, setFieldsValue, validate, saveOrUpdate, updateSchema }] = useForm({
  rowKey: 'userGoodsId',
  labelWidth: 250,
  labelCol: { span: 5 },
  schemas: nowBackRecordFormSchema,
  showActionButtonGroup: false,
});

const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async data => {
  resetFields();
  setDrawerProps({ confirmLoading: false });
  let record = data.record;
  getUserGoodsConsumptionInfo({
    userGoodsId: record.userGoodsId,
  }).then(result => {
    userGoodsInfo.data = result;
    setFieldsValue({
      ...result,
    });
  });
});

const userGoodsInfoSchema: DescItem[] = [
  {
    field: 'userName',
    label: '会员姓名',
    span: 3,
  },
  {
    field: 'mobile',
    label: '手机号',
    span: 3,
  },
  {
    field: 'vipCardNo',
    label: '会员卡号',
    span: 3,
  },
  {
    field: 'goodsName',
    label: '卡种',
    span: 3,
  },
  {
    field: 'payAmount',
    label: '支付金额',
    span: 3,
  },
  {
    field: 'payName',
    label: '支付方式',
    span: 3,
  },
  {
    field: 'initAmount',
    label: '电子金初始金额',
    span: 3,
  },
  {
    field: 'eleBalance',
    label: '电子金余额',
    span: 3,
  },
  {
    field: 'useTicketAmount',
    label: '电子金消费余额',
    span: 3,
  },
  {
    field: 'useVipAmount',
    label: '贵宾厅消费金额',
    span: 3,
  },
];

async function handleSubmit() {
  try {
    const values = await validate();
    setDrawerProps({ confirmLoading: true });
    await nowRefundBackcardSub(values);
    closeDrawer();
    emit('success', false);
  } finally {
    setDrawerProps({ confirmLoading: false });
  }
}
</script>
